<?php
/**
 * Easthv Zend Framework Project
 *
 *
 * LICENSE:
 *
 * @category   Zend
 * @package    Easthv_Model
 * @subpackage Easthv_Model_DbTable
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
*/

/**
 * Table data gateway Basic class
 * 
 * @uses       Zend_Db_Table_Abstract
 * @category   Zend
 * @package    Easthv_Model
 * @subpackage Easthv_Model_DbTable
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
 * @deprecated
 */
abstract class Easthv_Model_DbTable_DB extends Zend_Db_Table_Abstract
{
      /**
     * @var array  validation rules of database table
     */ 
    protected $_validate;

    /**
     * Get database table name
     *
     * @access public
     *
     * @return string database table name
     */
    public function getTableName()
    {
        return $this->_name;
    }

    /**
     * Get database table primary key
     *
     * @access public
     *
     * @return string database table primary key name
     */
    public function getPrimaryKey()
    {
        return $this->_primary;
    }

    /**
     * Get database table validation rules
     *
     * @access public
     *
     * @return array validation rules
     */
    public function getValidate()
    {
        return $this->_validate;
    }

    /**
     * Set database table validation rules
     *
     * @param  array $validate  validation rules array
     *
     * @access public
     *
     * @return void
     */
    public function setValidate($validate)
    {
        $this->_validate = $validate;
        return $this;
    }
    
    /**
     * Init tab cache
     *
     *
     * @access private
     *
     * @return boolean
     */
    private function _initTableCache()
    {
        $config = Zend_Registry::get('config');

        if (!$config['system']['cache']['enable'] || !$config['system']['cache']['dbmeta']) {
            return false;
        }

        $cacheType = $config['system']['cache']['backend']['type'];
        if (!in_array($cacheType,array('File','Sqlite','Memcached','Apc','Xcache','ZendPlatform'))) {
            return false;
        }

        $frontendOptions = array(
            'automatic_serialization' => true
        );

        $backendOptions = array();
        if ($cacheType == 'File') {
            $backendOptions = array(
                'cache_dir' => APPLICATION_PATH . $config['system']['path']['cache']['dbmeta']
            );
        }
        
        if ($cacheType == 'Memcached') {
            
            if ($config['system']['cache']['backend']['memcached']['host']) {
                $hosts = explode(',',$config['system']['cache']['backend']['memcached']['host']);
            }
            
            if ($config['system']['cache']['backend']['memcached']['port']) {
                $ports = explode(',',$config['system']['cache']['backend']['memcached']['port']);
            }
            
            if ($config['system']['cache']['backend']['memcached']['persistent']) {
                $persistents = explode(',',$config['system']['cache']['backend']['memcached']['persistent']);
            }

            if (!$hosts || !$ports || count($hosts) != count($ports)) {
                throw new Exception ('Memcached setting error.' );
            }
            
            $servers = array();

            foreach ($hosts as $key=>$host) {
                $servers[] = array(
                    'host' => $host,
                    'port' => $ports[$key],
                    'persistent' => $persistents[$key] == 1 ? true : false,
                );
            }
    
            $backendOptions = array(
                'servers' => $servers,
                'compression' => $config['system']['cache']['backend']['memcached']['compression'] == 1 ? true : false,
            );
        }

        $cache = Zend_Cache::factory('Core', $cacheType, $frontendOptions, $backendOptions);

        parent::setDefaultMetadataCache($cache);

        return true;
    }


    /**
     * construct function
     * init database table prefix
     * init dependent tables class name
     * init reference map class name
     *
     * @access public
     *
     * @return void
     */    
    public function __construct()
    {
        $config = Zend_Registry::get('config');
        $this->_name = isset($config['resources']['db']['prefix']) ? $config['resources']['db']['prefix'] . $this->_name : $this->_name ;

        if ($this->_dependentTables) {
            foreach($this->_dependentTables as $key => $value){
                $this->_dependentTables[$key] = 'Easthv_Model_DbTable_' . $value;
            }
        }

        if ($this->_referenceMap) {
            foreach($this->_referenceMap as $key => $value){
                $this->_referenceMap[$key]['refTableClass'] = 'Easthv_Model_DbTable_' . $value['refTableClass'];
            }        
        }

        $this->_initTableCache();
 
        parent::__construct();
    }

    

}
